package com.yxx.business.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yxx.business.entity.*;
import com.yxx.business.service.PhaseService;
import com.yxx.common.utils.DateUtils;
import com.yxx.common.core.domain.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import com.yxx.common.core.service.impl.ServiceImplPlus;
import com.yxx.business.mapper.TbMaterialProductionPhaseMapper;
import com.yxx.business.service.ITbMaterialProductionPhaseService;

/**
 * 建材生产运输阶段Service业务层处理
 *
 * @author yxx
 * @date 2025-08-16
 */
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor_ = {@Lazy, @Autowired})
public class TbMaterialProductionPhaseServiceImpl extends ServiceImplPlus<TbMaterialProductionPhaseMapper, TbMaterialProductionPhaseDo> implements ITbMaterialProductionPhaseService, PhaseService {

    private final TbMaterialProductionPhaseMapper tbMaterialProductionPhaseMapper;

    /**
     * 查询建材生产运输阶段分页结果
     *
     * @param tbMaterialProductionPhaseQueryDto 建材生产运输阶段查询实体
     * @return 建材生产运输阶段分页
     */
    @Override
    public PageResult<TbMaterialProductionPhaseVo> selectTbMaterialProductionPhaseVoPage(TbMaterialProductionPhaseQueryDto tbMaterialProductionPhaseQueryDto) {
        startPage();
        PageResult<TbMaterialProductionPhaseVo> page
                = super.getMyBatisPageResult(selectTbMaterialProductionPhaseDoList(tbMaterialProductionPhaseQueryDto), TbMaterialProductionPhaseVo.class);
        clearPage();
        return page;
    }

    /**
     * 查询建材生产运输阶段Vo列表
     *
     * @param tbMaterialProductionPhaseQueryDto 建材生产运输阶段查询实体
     * @return 建材生产运输阶段集合
     */
    @Override
    public List<TbMaterialProductionPhaseVo> selectTbMaterialProductionPhaseVoList(TbMaterialProductionPhaseQueryDto tbMaterialProductionPhaseQueryDto) {
        return super.convertList(tbMaterialProductionPhaseMapper.selectTbMaterialProductionPhaseList(tbMaterialProductionPhaseQueryDto), TbMaterialProductionPhaseVo.class);
    }

    /**
     * 查询建材生产运输阶段Do列表
     *
     * @param tbMaterialProductionPhaseQueryDto 建材生产运输阶段查询实体
     * @return 建材生产运输阶段集合
     */
    @Override
    public List<TbMaterialProductionPhaseDo> selectTbMaterialProductionPhaseDoList(TbMaterialProductionPhaseQueryDto tbMaterialProductionPhaseQueryDto) {
        return tbMaterialProductionPhaseMapper.selectTbMaterialProductionPhaseList(tbMaterialProductionPhaseQueryDto);
    }

    /**
     * 查询单个建材生产运输阶段
     *
     * @param productionId 建材生产运输阶段主键
     * @return 建材生产运输阶段单个
     */
    @Override
    public TbMaterialProductionPhaseVo selectTbMaterialProductionPhaseVoByProductionId(Long productionId) {
        return super.convertBean(tbMaterialProductionPhaseMapper.selectTbMaterialProductionPhaseByProductionId(productionId), TbMaterialProductionPhaseVo.class);
    }

    /**
     * 新增建材生产运输阶段
     *
     * @param tbMaterialProductionPhaseQueryDto 建材生产运输阶段编辑实体
     * @return 结果
     */
    @Override
    public int insertTbMaterialProductionPhase(TbMaterialProductionPhaseEditDto tbMaterialProductionPhaseQueryDto) {
        tbMaterialProductionPhaseQueryDto.setCreateById(getUserIdOrNotLogged());
        tbMaterialProductionPhaseQueryDto.setCreateBy(getUserNameOrNotLogged());
        tbMaterialProductionPhaseQueryDto.setCreateTime(DateUtils.getNowDate());
        return tbMaterialProductionPhaseMapper.insertTbMaterialProductionPhase(super.convertT(tbMaterialProductionPhaseQueryDto));
    }

    /**
     * 修改建材生产运输阶段
     *
     * @param productionId 建材生产运输阶段主键
     * @param tbMaterialProductionPhaseQueryDto 建材生产运输阶段编辑实体
     * @return 结果
     */
    @Override
    public int updateTbMaterialProductionPhase(Long productionId, TbMaterialProductionPhaseEditDto tbMaterialProductionPhaseQueryDto) {
        tbMaterialProductionPhaseQueryDto.setUpdateBy(getUserNameOrNotLogged());
        tbMaterialProductionPhaseQueryDto.setUpdateTime(DateUtils.getNowDate());
        return tbMaterialProductionPhaseMapper.updateTbMaterialProductionPhase(super.convertT(tbMaterialProductionPhaseQueryDto).setProductionId(productionId));
    }

    /**
     * 批量删除建材生产运输阶段
     *
     * @param productionIds 需要删除的建材生产运输阶段主键集合
     * @return 结果
     */
    @Override
    public int deleteTbMaterialProductionPhaseByProductionIds(List<Long> productionIds) {
        return tbMaterialProductionPhaseMapper.deleteTbMaterialProductionPhaseByProductionIds(productionIds);
    }

    /**
     * 删除单个建材生产运输阶段信息
     *
     * @param productionId 建材生产运输阶段主键
     * @return 结果
     */
    @Override
    public int deleteTbMaterialProductionPhaseByProductionId(Long productionId) {
        return tbMaterialProductionPhaseMapper.deleteTbMaterialProductionPhaseByProductionId(productionId);
    }

    @Override
    public TbEquipmentPhaseVo getPhase(TbEquipmentPhaseDto tbEquipmentPhaseDto) {
        return convertBean(tbMaterialProductionPhaseMapper.selectOne(
                        new LambdaQueryWrapper<TbMaterialProductionPhaseDo>().eq(TbMaterialProductionPhaseDo::getEngineeringId, tbEquipmentPhaseDto.getEngineeringId()).eq(TbMaterialProductionPhaseDo::getProjectId, tbEquipmentPhaseDto.getProjectId())),
                TbEquipmentPhaseVo.class);
    }

    @Override
    public int savePhase(TbEquipmentPhaseVo tbEquipmentPhaseVo) {
        Long l = tbMaterialProductionPhaseMapper.selectCount(new LambdaQueryWrapper<TbMaterialProductionPhaseDo>().eq(TbMaterialProductionPhaseDo::getEngineeringId, tbEquipmentPhaseVo.getEngineeringId()));
        if (l > 0) {
            tbMaterialProductionPhaseMapper.update(convertT(tbEquipmentPhaseVo), new LambdaUpdateWrapper<TbMaterialProductionPhaseDo>().eq(TbMaterialProductionPhaseDo::getEngineeringId, tbEquipmentPhaseVo.getEngineeringId()).eq(TbMaterialProductionPhaseDo::getProjectId, tbEquipmentPhaseVo.getProjectId()));
        } else {
            tbMaterialProductionPhaseMapper.insert(convertT(tbEquipmentPhaseVo));
        }
        return 1;
    }

    @Override
    public int updatePhase(Long id, TbEquipmentPhaseVo tbEquipmentPhaseVo) {
        return tbMaterialProductionPhaseMapper.updateById(convertT(tbEquipmentPhaseVo).setProductionId(id));
    }
}
